package com.duobao.gongneng;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;
import com.duobao.view.MainPanel;

import java.awt.event.*;

public class Qumingzi extends JPanel implements MouseListener,Runnable{
    DuobaoSwingLaunch game;
    Map heroMap;
    JLabel fanhui;
    JLabel fanhuiyouxi;
    JLabel jixu;

    JTextField jtf;
    JLabel tishi;
    public Qumingzi(DuobaoSwingLaunch game)
    {
        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);



        xiaoguo();
    }
    private void xiaoguo()
    {
        if(this.game.hero.getName().equals("无名氏"))
        {
            setTishi2();
            setJtf();
            setjixu();
        }else
        {
            setTishi1();
            setFanhuiyouxi();
        }
    }
    private void setJtf() {
        jtf = new JTextField(20);
        jtf.setFont(myFont.f3);
        JPanel jpn2 = new JPanel();
        jpn2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jpn2.add(jtf);
        this.add(jpn2);
    }
    private void setTishi1() {
        tishi=new JLabel("你已经过名字了！");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setTishi2() {
        tishi=new JLabel("身在江湖怎么能当一个无名小辈，请取一个响亮的名字吧！");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setjixu() {
        jixu=new JLabel();
        jixu.addMouseListener(this);
        jixu.setFont(myFont.f2);
        jixu.setForeground(Color.BLUE);
        jixu.setText("<html><u>"+"确定"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(jixu);
        this.add(jp3);
    }
    private void setFanhui() {
        fanhui=new JLabel();
        fanhui.addMouseListener(this);
        fanhui.setFont(myFont.f2);
        fanhui.setForeground(Color.BLUE);
        fanhui.setText("<html><u>"+"返回"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fanhui);
        this.add(jp3);
    }
    private void setFanhuiyouxi() {
        fanhuiyouxi=new JLabel();
        fanhuiyouxi.addMouseListener(this);
        fanhuiyouxi.setFont(myFont.f2);
        fanhuiyouxi.setForeground(Color.BLUE);
        fanhuiyouxi.setText("<html><u>"+"返回游戏"+"</u></html>");
        JPanel jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp4.add(fanhuiyouxi);
        this.add(jp4);
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        if(jl==tishi)
        {
            f=true;
        }
        return f;
    }

    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==this.jixu)
        {
            if(this.jtf.getText().length()>4 )
            {
                this.tishi.setText("不可以取太长的名字");

            }else if(this.jtf.getText().length()<2)
            {
                this.tishi.setText("不可以取太短的名字");

            }else if(IfChar.ifChar(this.jtf.getText()))
            {
                this.tishi.setText("不可以取这么奇怪的名字");
            }else
            {
                this.game.hero.setName(this.jtf.getText());
                this.game.setPanel(new Nannv(this.game));
            }
        }else if(e.getSource()==fanhuiyouxi)
        {
            this.game.setPanel(new MainPanel(this.game,1));
        }
    }
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void run() {
        // TODO Auto-generated method stub

    }

}
